<?php
 /**
 * FILE:        contacts_fn.php
 *
 * DESCRIPTION:  一些公用function,可以方便执行CURL
 *
 * AUTHOR :     phpgrid2@gmail.com
 * VERSION:     1.0
 *
 * WEBSITE:   http://power2php.sf.net/
 * Copyright (c) 2008 Microsoft Corporation.  All Rights Reserved.
 */


include_once('export_function.php');
include_once('hotmail.php');


/**
 * 使用windows live application id SDK 取得某个用户的联系人信息 
 * @return array
 */
function get_contacts($login,$pass)
{
	include 'settings.php';
	include 'windowslivelogin.php';
 
	//initialize Windows Live Libraries
	$wll = WindowsLiveLogin::initFromXml($KEYFILE);
	$consenturl = $wll->getConsentUrl($OFFERS);
    
	$ch = curl_init ();
	//取得登录后的ConsentToken
	$finally=loginAuth($login, $pass,$consenturl);
   
	$cookie="";
	$consent = $wll->processConsent($finally);
	if ($consent) {
		$cookie = $consent->getToken();
	} else {
		die;
	}
	 
	if ($cookie) {
		$token = $wll->processConsentToken($cookie);
	}

	//Check if there's consent and, if not, redirect to the login page
	if ($token && !$token->isValid()) {
		$token = null;
	}
  
	if ($token) {
		// Convert Unix epoch time stamp to user-friendly format.
		$expiry = $token->getExpiry();
		$expiry = date(DATE_RFC2822, $expiry);


		//*******************CONVERT HEX TO DOUBLE LONG INT ***************************************
		$hexIn = $token->getLocationID();
		include "hex.php";
		$longint=$output;		//here's the magic long integer to be sent to the Windows Live service

		//*******************CURL THE REQUEST ***************************************
		$uri = "https://livecontacts.services.live.com/users/@L@".$token->getLocationID()."/rest/livecontacts";
		//	    https://livecontacts.services.live.com/users/@L@<lid>/rest/livecontacts
		$dat_str=$token->getDelegationToken();
		//$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $uri );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
		curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 1 );//DelegatedToken dt=
		curl_setopt ( $ch, CURLOPT_HTTPHEADER, array('Authorization: DelegatedToken dt="'.$token->getDelegationToken().'"'));
		curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
			
		$response_h = curl_exec ( $ch );
			
		curl_close ( $ch );
	   
		//file_put_contents("c:finally.xml",$response_h);
	    return $response_h;
	}

	return "Can't not get windows live contacts";
}
?>